package logic;

import gui.GuiMain;

import java.util.ArrayList;
import java.util.Collections;

import memory.GenericEvents;


public class Sort {
	private static ArrayList<GenericEvents> task;
	private static ArrayList<GenericEvents> deadline;
	private static ArrayList<GenericEvents> event;

	//@author A0096677H
	// Sorts all items in memory by urgency.
	// Items arranged by Event, Deadline, Task
	// Event sorted by earliest start date followed by end date
	// Deadline sorted by earliest due date
	// Task sorted by alphabet
	public static void sort(ArrayList<GenericEvents> list){
		task = new ArrayList<GenericEvents>();
		deadline = new ArrayList<GenericEvents>();
		event = new ArrayList<GenericEvents>();
		splitMemory(list);

		sortAllList();

		joinList(list);

		GuiMain.updateQuickView();

	}

	//@author A0096677H
	// Reform list by adding Event, Deadline, Task items in sorted order
	private static void joinList(ArrayList<GenericEvents> list) {
		list.removeAll(list);
		assert list.size() == 0;
		for (int i=0; i<event.size();i++){
			list.add(event.get(i));
		}
		for (int i=0; i<deadline.size();i++){
			list.add(deadline.get(i));
		}
		for (int i=0; i<task.size();i++){
			list.add(task.get(i));
		}
	}

	//@author A0096677H
	// Sorts the 3 lists
	private static void sortAllList() {
		Collections.sort(task);
		Collections.sort(deadline);
		Collections.sort(event);
	}

	//@author A0096677H
	// Split all items into their own list for sorting
	private static void splitMemory(ArrayList<GenericEvents> list) {
		for (int i=0; i<list.size(); i++) { 
			switch(list.get(i).getType()) {
			case "Task":
				task.add(list.get(i));
				break;
			case "Deadline":
				deadline.add(list.get(i));
				break;
			case "Event":
				event.add(list.get(i));
				break;
			}
		}
	}
}
